import { Component, OnInit } from '@angular/core';
import {FormControl, FormGroup, NgForm} from '@angular/forms';
import {ActivatedRoute, Router} from '@angular/router';
import {HttpService} from '../../service/http.service';

const MOBILE_REGEXP = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
  tips: String;
  tipsBoxStatus = false;
  constructor(private router: Router, private httpService: HttpService) {
  }

  ngOnInit() {
  }

  onSubmit(theForm: NgForm) {
    if ( theForm['email'] == null || theForm['email'] === '' ) {
      this.tips = '邮箱不能为空';
      this.tipsBoxStatus = true;
      return;
    }
    if ( !MOBILE_REGEXP.test(theForm['email']) ) {
      this.tips = '邮箱格式错误';
      this.tipsBoxStatus = true;
      return;
    }
    if ( theForm['password'] == null || theForm['password'] === '' ) {
      this.tips = '密码不能为空';
      this.tipsBoxStatus = true;
      return;
    }
    if ( theForm['confirmpassword'] !== theForm['password'] ) {
      this.tips = '两次密码输入不一致';
      this.tipsBoxStatus = true;
      return;
    }

    this.httpService.postUser(theForm['email'], theForm['password']).then(r => {
      if ( r.code === 100) {
        this.tips = r.msg;
        this.tipsBoxStatus = true;
        return;
      }
      if ( r.code === 0 ) {
        this.router.navigate(['/']);
      }
    });

  }

  onclicklose() {
    this.tipsBoxStatus = false;
  }
}
